Skip to content

fix(session): accept each JWT algorithm that the JWKS key can verify - #790

Open
gjtorikian wants to merge 1 commit into
mainfrom
fix/session-jwt-key-algorithms
Open

gjtorikian wants to merge 1 commit into
mainfrom
fix/session-jwt-key-algorithms

Conversation

@gjtorikian

Copy link
Copy Markdown
Contributor

Description

On main, the session verifier accepts an access token only if its alg header is RS256 or ES256. #788 added the list of these two names, and #788 is not in a release. This pull request removes the list. The SDK code now has no list of algorithm names. The JWKS key that the kid of the token names decides the algorithm. PyJWT makes each decision from the alg member, the type and the curve of that key.

How the key decides

The JWKS client of the SDK gives the JWKS entries to PyJWK one at a time and skips an entry that PyJWT cannot read. PyJWK gives one algorithm to a key. That algorithm is the alg member of the key, or the PyJWT default for the key type and curve.

The SDK selects the key by kid. A key with an alg member verifies that algorithm only. For a key without alg, PyJWT must read the key for the token alg. PyJWT rejects a key whose type or curve is not correct for that algorithm. jwt.decode gets the PyJWK and no algorithms argument, thus PyJWT accepts the algorithm of that key only.

An unsigned token and a MAC token never verify. PyJWT reads no RSA, EC or OKP entry for such an algorithm, and the SDK rejects each key that is not a public key. The SDK also rejects these tokens before the JWKS request. For that check, it uses the PyJWT classes NoneAlgorithm and HMACAlgorithm.

Checks in SDK code

  • Entry read. The SDK reads the JWKS entries one at a time and skips an entry for each error type that PyJWT raises. The JWKS client keeps its keys if a new JWKS has no readable key. PyJWKSet raises an error for the full set if, for example, the alg member of one entry is a list.
  • Entry without alg. If PyJWK(jwk) cannot read such an entry, the SDK gives it to the algorithms of the PyJWT registry (get_default_algorithms()), one at a time. The first algorithm that reads the entry becomes the algorithm of the key. PyJWK has no default algorithm for an OKP key that is not Ed25519.
  • alg member. The SDK gives the token alg to PyJWK only for a key that has no alg member. PyJWK(jwk, algorithm) does not compare its algorithm argument with that member.
  • kid and algorithm. The SDK selects the first key with the kid that is a key for the token alg. It makes the same check on a key from a replacement for Session.jwks. PyJWKClient selects a key by kid only.
  • Key type and curve. The SDK calls Algorithm.prepare_key before jwt.decode and rejects the key for each error. jwt.decode raises InvalidKeyError for a key on another curve, and that error is not an InvalidTokenError.
  • Public key and use. The key must be an object of the cryptography union PublicKeyTypes, and use must be absent or sig. PyJWT reads an oct entry as a MAC secret and an entry with private members as a private key. jwt.decode does not read use.
  • crit header. The SDK rejects a token that has a crit header, as on main. PyJWT accepts a token with "crit": ["b64"].
  • Early check. Before the JWKS request, the token alg must be a name of the PyJWT registry whose class is not HMACAlgorithm or NoneAlgorithm. This check closes no gap of PyJWT. It keeps the count of JWKS requests at 0 for a token that it rejects.
  • Error types. The SDK changes each error of the header read and of jwt.decode that is not an InvalidTokenError to an InvalidTokenError. PyJWT does not change UnicodeEncodeError (a lone surrogate in the token) or ValueError (an RSA key too small for the hash).

Algorithms

  • The tests of this pull request verify the algorithms below with real signatures. The tests make each key at test time.
  • An RSA key of 2048 bits: RS256, RS384, RS512, PS256, PS384, PS512. RSA keys of 3072 and 4096 bits: RS256. EC keys: ES256 (P-256), ES384 (P-384), ES512 (P-521), ES256K (secp256k1). OKP keys: EdDSA (Ed25519 and Ed448).
  • Versions: PyJWT 2.15.1 and cryptography 50.0.0 on Python 3.10, 3.11, 3.12, 3.13 and 3.14, and PyJWT 2.15.0 on Python 3.10. Another algorithm verifies only if PyJWT and cryptography support it for the key.

No change

  • No public name, parameter or return type of workos.session changes. A comparison of the module on main and with this change shows 10 public names with equal signatures and members.
  • In the tests of this pull request, each token that the SDK or PyJWT rejects gives the failure reason invalid_jwt. The results for a failed JWKS request do not change. authenticate() raises PyJWKClientConnectionError or OSError.
  • The claims checks and the JWKS cache of 300 seconds do not change. The refetch for an unknown kid (one time at most in each 30 seconds) does not change. pyproject.toml, uv.lock and the workflows do not change.

Behavior changes

  • A token with a correct signature now verifies for each algorithm of the Algorithms section. On main, the result is invalid_jwt for each algorithm other than RS256 and ES256. A key whose alg is RS256 verifies RS256 only, as on main. With a replacement PyJWKClient in Session.jwks, a key verifies only the algorithm that PyJWT gave to it. Thus PS256 with an RSA key without alg gives invalid_jwt there, and the JWKS client of the SDK accepts it.
  • On main, only an RS256 or ES256 token can cause a JWKS request. Now each token whose alg is a public key algorithm of PyJWT can cause one. Example: a PS256 token for a key whose alg is RS256 gives invalid_jwt with 1 request (main: 0). none, a MAC name and a name that PyJWT does not have cause no request, as on main. For a JWKS whose only key is an Ed448 key without alg, 3 calls with an RS256 token cause 1 request (main: 3).
  • A token text that PyJWT cannot encode (a lone surrogate) now gives invalid_jwt. On main, authenticate() raises UnicodeEncodeError and refresh() gives the error text as the reason.
  • An error of jwt.decode that is not an InvalidTokenError now gives invalid_jwt. Example: with jwt_leeway="60" (a str), authenticate() raises TypeError on main.
  • This change removes the private names Session._JWK_ALGORITHMS, AsyncSession._JWK_ALGORITHMS, workos.session._ACCESS_TOKEN_ALGORITHMS and workos.session._key_agrees_with_algorithm. No SDK code reads _JWK_ALGORITHMS, thus a value that code sets there has no effect.

Tests

  • Each algorithm of the Algorithms section verifies through authenticate() and refresh() of Session and AsyncSession. One test adds an algorithm class to the PyJWT registry, and the SDK verifies its token with no change of SDK code.
  • Key confusion gives invalid_jwt. The cases: an alg for another key type, an ECDSA alg for another curve, an alg that differs from the alg member.
  • none, HS256, HS384 and HS512 tokens give invalid_jwt. The cases use the public key bytes as the secret, and an oct entry with the kid of the token. A key with private members verifies no token.
  • An ECDSA signature of an incorrect length or in DER form gives invalid_jwt. A changed bit and an RSA signature of another padding or hash give the same result.
  • With entries of an unknown kty or crv and malformed entries in the JWKS, the other keys verify. A token for a skipped key gives invalid_jwt. The jwk, jku, x5u and x5c headers of a token have no effect.
  • The removal of each check of the section "Checks in SDK code" makes tests fail (15 removals, each on a copy of the source). tests/test_session.py has 1243 tests (main: 507). The test file of main runs against this change with 492 passes and 15 failures. Those 15 tests use the RS256 and ES256 list, or assert a result of that list. The full suite passes on Python 3.10 to 3.14 (5231 tests).

Documentation

Does this require changes to the WorkOS Docs? E.g. the API Reference or code snippets need updates.

[ ] Yes

If yes, link a related docs PR and add a docs maintainer as a reviewer. Their approval is required.

A list from #788 permits only RS256 and ES256 access tokens.
This change removes that list.
The JWKS key that the kid of the token names decides the algorithm.
PyJWT makes each decision from the alg, type and curve of that key.
A test adds an algorithm to the PyJWT registry, and its token verifies.
The SDK uses public keys only. Unsigned and MAC tokens never verify.
A token that PyJWT cannot verify gives the failure reason invalid_jwt.
@gjtorikian
gjtorikian requested a review from a team as a code owner December 4, 2026 09:55
@greptile-apps

greptile-apps Bot commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 5/5RetriggerView in Greptile

[High impact] The PR appears safe to merge; no actionable issues were found.

Summary

Session verification now accepts the public-key algorithms supported by the selected JWKS key instead of a fixed RS256 and ES256 list.

  • Session tokens can use each public-key algorithm their JWKS key supports.
  • Token reading and verification errors now use the usual invalid-JWT result.

No actionable issues were found. Tests were inspected but not run.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Read token header] --> B{Known public-key algorithm and no crit?}
  B -->|No| F[Return invalid_jwt]
  B -->|Yes| C[Find JWKS key by kid and algorithm]
  C --> D{Public key with matching algorithm and allowed use?}
  D -->|No| F
  D -->|Yes| E[Verify signature and claims with PyJWT]
  E --> G[Return session result]
Loading

Reviews (1) · Last reviewed commit: "fix(session): accept each JWT algorithm ..." · Reviewed by Greptile

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant